﻿
// ProjectAADlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "ProjectAA.h"
#include "ProjectAADlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//ImgCap *imgCap;
//ImgFile imgFile; 
//CTS CTSCam;

// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CProjectAADlg 对话框



CProjectAADlg::CProjectAADlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_PROJECTAA_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CProjectAADlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_listSave);
	DDX_Control(pDX, IDC_CHECK_10AUTO2, Button1);
	DDX_Control(pDX, IDC_BUTTON4, Button2);
	DDX_Control(pDX, IDC_BUTTON2, Button3);
	DDX_Control(pDX, IDC_BUTTON1, Static1);
	DDX_Control(pDX, IDC_STATIC_101, Static2);
	DDX_Control(pDX, IDC_STATIC_2, Static3);
	DDX_Control(pDX, IDC_STATIC_10STATUS, Static4);
	DDX_Control(pDX, IDC_STATIC1, Static5);
	DDX_Control(pDX, IDC_STATIC2, Static6);
	DDX_Control(pDX, IDC_STATIC_10SN, Static7);
	DDX_Control(pDX, IDC_STATIC_10SN2, Static8);
	DDX_Control(pDX, IDC_STATIC3, Static9);
	DDX_Control(pDX, IDC_STATIC4, Static10);
	DDX_Control(pDX, IDC_STATIC5, Static11);
	DDX_Control(pDX, IDC_STATIC_54, Static12);
	DDX_Control(pDX, IDC_STATIC_53, Static13);
	DDX_Control(pDX, IDC_STATIC_51, Static14);
	DDX_Control(pDX, IDC_STATIC6, Static15);
	DDX_Control(pDX, IDC_STATIC7, Static16);
	DDX_Control(pDX, IDC_STATIC_56, Static17);
	DDX_Control(pDX, IDC_BUTTON5, Button4);
}

BEGIN_MESSAGE_MAP(CProjectAADlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(ID_32771, &CProjectAADlg::OnOpenLogDlg)
	ON_COMMAND(ID_32773, &CProjectAADlg::OnOpenMotorDlg)
	ON_WM_SIZE()
	ON_COMMAND(ID_32774, &CProjectAADlg::OnOpenTcpipDlg)
	ON_MESSAGE(WM_SEND_SOCKET_SERVER_MSG_TO_MAIN_UI, &CProjectAADlg::OnReceiveServerMsg)
	ON_WM_CTLCOLOR()
	ON_COMMAND(ID_32783, &CProjectAADlg::OnOpenProjectDlg)
	ON_COMMAND(ID_32784, &CProjectAADlg::OnOpenIODlg)
	ON_COMMAND(ID_32785, &CProjectAADlg::OnOpenControlDlg)
	ON_COMMAND(ID_32787, &CProjectAADlg::OnOpenRS232Dlg)
	ON_BN_CLICKED(IDC_CHECK_10AUTO2, &CProjectAADlg::OnBnClickedCheck10auto2)
	ON_BN_CLICKED(IDC_BUTTON1, &CProjectAADlg::OnBnClickedButton1)
	ON_COMMAND(ID_32788, &CProjectAADlg::OnLoginDlg)
	ON_BN_CLICKED(IDC_BUTTON2, &CProjectAADlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON4, &CProjectAADlg::OnBnClickedButton4)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST1, &CProjectAADlg::OnLvnItemchangedList1)
	ON_NOTIFY(NM_CUSTOMDRAW, IDC_LIST1, &CProjectAADlg::OnNMCustomdrawList1)
	ON_BN_CLICKED(IDC_BUTTON5, &CProjectAADlg::OnBnClickedButton5)
	ON_COMMAND(ID_32789, &CProjectAADlg::OnOpenMesDlg)
	ON_COMMAND(ID_MENU_SYS, &CProjectAADlg::OnHandleOpenSetBarcode)
	ON_BN_CLICKED(IDC_CHECK_END_NO_SCAN, &CProjectAADlg::OnBnClickedCheckEndNoScan)
END_MESSAGE_MAP()


// CProjectAADlg 消息处理程序

BOOL CProjectAADlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	unique_mutex = CreateMutex(NULL, FALSE, L"LJ120");
	DWORD errCode = GetLastError();
	if (errCode == ERROR_ALREADY_EXISTS) {
		Sleep(500);
		CloseHandle(unique_mutex);
		MessageBox(L"LJ镭雕已打开！！，请关闭", L"Warning", 0);
		::TerminateProcess(::GetCurrentProcess(), NULL);//终止原先程序
	}
	Sleep(1500);

	// 设置字体参数

	m_editFont.CreateFont(50, 0, 4, 0, FW_NORMAL, FALSE, FALSE, 0,
		ANSI_CHARSET,
		OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY,
		DEFAULT_PITCH | FF_SWISS,
		_T("Arial"));
	Static4.SetFont(&m_editFont);

	m_editFont4.CreateFont(80, 40, 4, 0, FW_NORMAL, FALSE, FALSE, 0,
		ANSI_CHARSET,
		OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY,
		DEFAULT_PITCH | FF_SWISS,
		_T("Arial"));

	GetDlgItem(IDC_STATIC_ERR)->SetFont(&m_editFont4);
	Button1.SetFont(&m_editFont); //

	m_editFont1.CreateFont(50, 0, 4, 0, FW_NORMAL, FALSE, FALSE, 0,
		ANSI_CHARSET,
		OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY,
		DEFAULT_PITCH | FF_SWISS,
		_T("Arial"));

	m_editFont20.CreateFont(20, 0, 4, 0, FW_NORMAL, FALSE, FALSE, 0,
		ANSI_CHARSET,
		OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY,
		DEFAULT_PITCH | FF_SWISS,
		_T("Arial"));

	Static7.SetFont(&m_editFont20);
	Static8.SetFont(&m_editFont1);

	m_editFont2.CreateFontW(30, 0, 4, 0, 500, FALSE, FALSE, 0,
		ANSI_CHARSET,
		OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY,
		DEFAULT_PITCH | FF_SWISS,
		_T("Arial"));
	Static2.SetFont(&m_editFont2); //
	Static3.SetFont(&m_editFont2); //
	Static5.SetFont(&m_editFont2); //
	Static6.SetFont(&m_editFont2); //
	Static9.SetFont(&m_editFont2); //
	Static10.SetFont(&m_editFont2); //
	Static11.SetFont(&m_editFont2); //
	Static12.SetFont(&m_editFont2); //
	Static13.SetFont(&m_editFont2); //
	Static14.SetFont(&m_editFont2); //
	Static15.SetFont(&m_editFont2); //
	Static16.SetFont(&m_editFont2); //
	Static17.SetFont(&m_editFont2); //
	Button2.SetFont(&m_editFont2);
	Button3.SetFont(&m_editFont2);
	Button4.SetFont(&m_editFont2);

	m_editFont3.CreateFontW(30, 10, 4, 0, 400, FALSE, FALSE, 0,
		ANSI_CHARSET,
		OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY,
		DEFAULT_PITCH | FF_SWISS,
		_T("Arial"));

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	gSystemMgr.GeneralSettingIniInit();//读取型号配置
	

	m_menu.LoadMenuW(IDR_MENU1);
	this->SetMenu(&m_menu);

	SetUiHandle();

	gSystemMgr.m_ptrMainUI = this;

	if (m_cLogDlg == nullptr)
	{
		m_cLogDlg = new CLogDlg;
		m_cLogDlg->Create(IDD_DIALOG_LOG, this);
	}
	m_cLogDlg->ShowWindow(SW_HIDE);

	gSystemMgr.LoginConfigIniInit();
	gSystemMgr.RS232ConfigIniInit();
	gSystemMgr.MesConfigIniInit();
	gSystemMgr.ReadSetBarcodeConfig();
	

	bool bInit = gSystemMgr.MotorIO_Init();//轴卡初始化
	if (bInit)
	{
		gSystemMgr.m_cIo.StartGetIoInputThread();
	}

	GetClientRect(&m_rcUiRect);


	gSystemMgr.StartOtherThread(); 
	gSystemMgr.StartAutoThread();
	gSystemMgr.StartLightControlThread();

	if (m_cLoginDlg != nullptr)
	{
		m_cLoginDlg->ShowWindow(SW_SHOW);
	}
	if (m_cLoginDlg == nullptr)
	{
		m_cLoginDlg = new CLoginDlg;
		m_cLoginDlg->Create(IDD_DIALOG_LOGIN, this);
	}
	m_cLoginDlg->ShowWindow(SW_SHOW);

	this->InitUi();
	this->ShowWindow(SW_MAXIMIZE);

	return TRUE;
}


void CProjectAADlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

void CProjectAADlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CRect rect;
		CPaintDC dc(this);
		GetClientRect(rect);
		dc.FillSolidRect(rect, RGB(169, 179, 195));//设置为绿色背景  

		CDialogEx::OnPaint();
	}
}

HCURSOR CProjectAADlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CProjectAADlg::SetUiHandle()
{
	m_menu.EnableMenuItem(ID_32787, MF_DISABLED);
	m_menu.EnableMenuItem(ID_32784, MF_DISABLED);
	m_menu.EnableMenuItem(ID_32785, MF_DISABLED);

}


void CProjectAADlg::OnOpenLogDlg()
{
	if (m_cLogDlg != nullptr)
	{
		m_cLogDlg->ShowWindow(SW_SHOW);
	}
}


void CProjectAADlg::OnOpenMotorDlg()
{
}


void CProjectAADlg::Repaint(CWnd * pWnd, int last_Width, int now_Width, int last_Height, int now_Height, int cnt)
{
	if (pWnd)
	{
		CRect rect;
		CRect rect2;
		pWnd->GetWindowRect(&rect2);
		rect = OriRect[cnt];
		int n = 62;
		int m = 8;
		//rect2.top = rect2.top - n;
		//rect2.bottom = rect2.bottom - n;
		ScreenToClient(&rect2);

		double proportion_x = now_Width / (double)last_Width;
		double proportion_y = now_Height / (double)last_Height;

		rect.left = (long)(rect.left*proportion_x + 0.5 - m * proportion_x);
		rect.right = (long)(rect.left+ RectW[cnt] *proportion_x + 0.5);
		rect.top = (long)(rect.top *proportion_y + 0.5 - n * proportion_y);
		rect.bottom = (long)(rect.top+ RectH[cnt] *proportion_y + 0.5);
		pWnd->MoveWindow(&rect);
	}

}

void CProjectAADlg::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);

	if (nType == SIZE_RESTORED || nType == SIZE_MAXIMIZED)//窗体大小发生变动。处理程序
	{
		float fsp[2];
		POINT Newp; //获取现在对话框的大小
		CRect recta;
		GetClientRect(&recta); //取客户区大小   
		Newp.x = recta.right - recta.left;
		Newp.y = recta.bottom - recta.top;
		fsp[0] = (float)Newp.x / Old.x;
		fsp[1] = (float)Newp.y / Old.y;
		CRect Rect;
		int woc;
		CPoint OldTLPoint, TLPoint; //左上角
		CPoint OldBRPoint, BRPoint; //右下角
		HWND hwndChild = ::GetWindow(m_hWnd, GW_CHILD); //列出所有控件   
		while (hwndChild)
		{
			woc = ::GetDlgCtrlID(hwndChild);//取得ID
			GetDlgItem(woc)->GetWindowRect(Rect);
			ScreenToClient(Rect);
			OldTLPoint = Rect.TopLeft();
			TLPoint.x = long(OldTLPoint.x*fsp[0]);
			TLPoint.y = long(OldTLPoint.y*fsp[1]);
			OldBRPoint = Rect.BottomRight();
			BRPoint.x = long(OldBRPoint.x *fsp[0]);
			BRPoint.y = long(OldBRPoint.y *fsp[1]); //高度不可读的控件（如:combBox),不要改变此值.
			Rect.SetRect(TLPoint, BRPoint);
			GetDlgItem(woc)->MoveWindow(Rect, TRUE);
			hwndChild = ::GetWindow(hwndChild, GW_HWNDNEXT);
		}
		Old = Newp;
	}
	Invalidate();//更新窗口
}


void CProjectAADlg::OnOpenTcpipDlg()
{
}


void CProjectAADlg::OnCancel()
{
	delete m_cLogDlg;
	delete m_cProjectDlg;

	ioc_board_close();
	CDialogEx::OnCancel();
}

LRESULT CProjectAADlg::OnReceiveServerMsg(WPARAM wParam, LPARAM lParam)
{
	CString cstrp = (BSTR)wParam;
	SysFreeString((BSTR)wParam);

	gSystemLog.DisplayLogAndSave(cstrp.GetBuffer(), LV_EVENT);

	std::vector<wstring> vResultItem = _utils::SplitString(cstrp.GetBuffer(), L",");

	if (vResultItem.size() < 2)
	{
		gSystemLog.DisplayLogAndSave(L"Client 返回的数据有误", LV_ERROR);
		return LRESULT();
	}

	return LRESULT();
}
HBRUSH CProjectAADlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr1 = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);

	DeleteObject(hbr);

	UINT id = pWnd->GetDlgCtrlID();
	CString Cstr; 
	if (id == IDC_CHECK_10AUTO2)
	{
		if (gSystemMgr.m_bAutoFlag_10) {
			hbr = CreateSolidBrush(RGB(0, 255, 0));
			pDC->SetTextColor(RGB(0, 255, 0));
		}
		else 
		{
			hbr = CreateSolidBrush(RGB(255, 0, 0));
			pDC->SetTextColor(RGB(255, 0, 0));
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_53)
	{
		GetDlgItem(id)->GetWindowTextW(Cstr);
		hbr = CreateSolidBrush(RGB(0, 255, 0));
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_51)
	{
		GetDlgItem(id)->GetWindowTextW(Cstr);
		hbr = CreateSolidBrush(RGB(255, 0, 0));
		pDC->SetBkMode(TRANSPARENT);
	}
	else if (id == IDC_STATIC_ERR)
	{
		CString str;
		GetDlgItemText(IDC_STATIC_ERR, str);
		str.ReleaseBuffer();
		if (str == L"OK" || str.Find(L"成功") >= 0)
		{
			hbr = CreateSolidBrush(RGB(0, 255, 0));
		}
		else
		{ 
			if (str.GetLength() > 0)
			{
				hbr = CreateSolidBrush(RGB(255, 0, 0));
			}
			else
			{
				hbr = CreateSolidBrush(RGB(255, 255, 255));
			}
		}
		pDC->SetBkMode(TRANSPARENT);
	}
	else
	{
		hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

	}
	hbr1 = hbr;
	return hbr1;
}


void CProjectAADlg::OnOpenProjectDlg()
{
	if (m_cProjectDlg != nullptr)
	{
		delete m_cProjectDlg;
		m_cProjectDlg = nullptr;
	}
	if (m_cProjectDlg == nullptr)
	{
		m_cProjectDlg = new CChangeProjectDlg;
		m_cProjectDlg->Create(IDD_DIALOG_PROJECT, this);
	}
	m_cProjectDlg->ShowWindow(SW_SHOW);
}



void CProjectAADlg::OnOpenIODlg()
{
	if (m_cIODlg != nullptr)
	{
		delete m_cIODlg;
		m_cIODlg = nullptr;
	}
	if (m_cIODlg == nullptr)
	{
		m_cIODlg = new CIODlg;
		m_cIODlg->Create(IDD_DIALOG_IO, this);
	}
	m_cIODlg->ShowWindow(SW_SHOW);
}

void CProjectAADlg::OnOpenControlDlg()
{
	if (m_cControlDlg != nullptr)
	{
		delete m_cControlDlg;
		m_cControlDlg = nullptr;
	}
	if (m_cControlDlg == nullptr)
	{
		m_cControlDlg = new CControlDlg;
		m_cControlDlg->Create(IDD_DIALOG_CONTROL, this);
	}
	m_cControlDlg->ShowWindow(SW_SHOW);
}

void CProjectAADlg::OnOpenRS232Dlg()
{
	if (m_cRS232Dlg != nullptr)
	{
		delete m_cRS232Dlg;
		m_cRS232Dlg = nullptr;
	}
	if (m_cRS232Dlg == nullptr)
	{
		m_cRS232Dlg = new CRS232Dlg;
		m_cRS232Dlg->Create(IDD_DIALOG_RS232, this);
	}
	m_cRS232Dlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnBnClickedCheck10auto2()
{
	CString cstr;
	CButton* pCheckBox = (CButton*)GetDlgItem(IDC_CHECK_10AUTO2);
	int nFlag = pCheckBox->GetCheck();
	if (nFlag == BST_CHECKED)
	{
		gSystemMgr.m_bAutoFlag_10 = 1;
		cstr.Append(L"开启自动");
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
		GetDlgItem(IDC_CHECK_10AUTO2)->SetWindowTextW(L"自动开启");
	}
	if (nFlag == BST_UNCHECKED)
	{
		gSystemMgr.m_bAutoFlag_10 = 0;
		cstr.Append(L"关闭自动");
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
		GetDlgItem(IDC_CHECK_10AUTO2)->SetWindowTextW(L"自动关闭");
	}
}



BOOL CProjectAADlg::PreTranslateMessage(MSG* pMsg)
{
	if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_SPACE) return TRUE; //如果是空格消息，返回；
	if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE) return TRUE; //如果是Esc消息，返回；
	if (gSystemMgr.m_nGetCodeFlag == 0 && pMsg->message == WM_KEYDOWN) {
		USES_CONVERSION;
		if (pMsg->wParam == VK_RETURN && gSystemMgr.m_nBarcodeLength > 0)
		{
			gSystemLog.DisplayLogAndSave(A2T(gSystemMgr.Barcode), LV_EVENT);
			SetDlgItemTextW(IDC_STATIC_10SN, A2T(gSystemMgr.Barcode));
			gSystemMgr.m_nGetCodeFlag = 1;
			return true;
		}

		if (gSystemMgr.m_nBarcodeLength >= 120) {
			gSystemMgr.m_nGetCodeFlag = -1;
			gSystemMgr.m_nBarcodeLength = 0;
			return true;
		}
		switch (pMsg->wParam)
		{

			case VK_3:
				if (GetKeyState(VK_SHIFT) < 0)
				{
					gSystemMgr.Barcode[gSystemMgr.m_nBarcodeLength] = '#';
					gSystemMgr.m_nBarcodeLength++;
				}else
				{
					gSystemMgr.Barcode[gSystemMgr.m_nBarcodeLength] = '3';
					gSystemMgr.m_nBarcodeLength++;
				}
				break;

			case VK_4:
				if (GetKeyState(VK_SHIFT) < 0)
				{
					gSystemMgr.Barcode[gSystemMgr.m_nBarcodeLength] = '$';
					gSystemMgr.m_nBarcodeLength++;
				}
				else
				{
					gSystemMgr.Barcode[gSystemMgr.m_nBarcodeLength] = '4';
					gSystemMgr.m_nBarcodeLength++;
				}
				break;


		case VK_A:case VK_B:case VK_C:case VK_D:
		case VK_E:case VK_F:case VK_G:case VK_H:
		case VK_I:case VK_J:case VK_K:case VK_L:
		case VK_M:case VK_N:case VK_O:case VK_P:
		case VK_Q:case VK_R:case VK_S:case VK_T:
		case VK_U:case VK_V:case VK_W:case VK_X:
		case VK_Y:case VK_Z:case VK_0:case VK_1:
		case VK_2:case VK_5:
		case VK_6:case VK_7:case VK_8:case VK_9: 
			gSystemMgr.Barcode[gSystemMgr.m_nBarcodeLength] = pMsg->wParam;
			gSystemMgr.m_nBarcodeLength++;
			break;

		case 189:
			gSystemMgr.Barcode[gSystemMgr.m_nBarcodeLength] = '-';
			gSystemMgr.m_nBarcodeLength++;
			break;

		}

		if (gSystemMgr.m_nBarcodeLength > 0)
		{
			SetDlgItemTextW(IDC_STATIC_10SN, A2T(gSystemMgr.Barcode));
		}
	}

	if (pMsg->message == WM_KEYDOWN/* && pMsg->wParam == VK_RETURN*/) return TRUE;

	return CDialogEx::PreTranslateMessage(pMsg);
}

void CProjectAADlg::OnBnClickedButton1()
{
	SetDlgItemTextW(IDC_STATIC_10SN, L"...");
	gSystemMgr.m_nGetCodeFlag = 0;
	memset(gSystemMgr.Barcode, 0, sizeof(gSystemMgr.Barcode)); 
	USES_CONVERSION;
	SetDlgItemTextW(IDC_STATIC_10SN, A2T(gSystemMgr.Barcode));
	gSystemMgr.m_nBarcodeLength = 0;
}


void CProjectAADlg::OnLoginDlg()
{
	if (m_cLoginDlg != nullptr)
	{
		delete m_cLoginDlg;
		m_cLoginDlg = nullptr;
	}
	if (m_cLoginDlg == nullptr)
	{
		m_cLoginDlg = new CLoginDlg;
		m_cLoginDlg->Create(IDD_DIALOG_LOGIN, this);
	}
	m_cLoginDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnBnClickedButton2()
{

	if (IDYES == AfxMessageBox(L"确定 清空计数 吗？", MB_YESNO))
	{
		CString cstr;
		gSystemMgr.NGCnt = 0;
		gSystemMgr.OKCnt = 0;
		cstr.Format(L"%d", gSystemMgr.NGCnt);
		gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"CNT", L"NGCnt", cstr);
		gSystemMgr.m_cRS232ConfigIni.SaveConfigIni(L"CNT", L"OKCnt", cstr);
	}
}


void CProjectAADlg::OnBnClickedButton4()
{
	if (gSystemMgr.Y06 == OFF) 
	{
		gSystemMgr.m_cIo.SetOutputIo(6, ON);
	}
	else 
	{
		gSystemMgr.m_cIo.SetOutputIo(6, OFF);
	}
}


void CProjectAADlg::OnLvnItemchangedList1(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
}


void CProjectAADlg::OnNMCustomdrawList1(NMHDR *pNMHDR, LRESULT *pResult)
{
	NMLVCUSTOMDRAW* lplvcd = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
	int nIRow = int(lplvcd->nmcd.dwItemSpec);
	if (lplvcd->nmcd.dwDrawStage == CDDS_PREPAINT)
	{
		*pResult = CDRF_NOTIFYITEMDRAW;
		return;
	}
	if (lplvcd->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)
	{
		lplvcd->clrTextBk = RGB(200, 220, 250);
		//lplvcd->clrTextBk =TRANSPARENT;
		*pResult = CDRF_NOTIFYSUBITEMDRAW;
		return;
	}
	else if (lplvcd->nmcd.dwDrawStage == (CDDS_SUBITEM | CDDS_PREPAINT | CDDS_ITEMPREPAINT))
	{
		COLORREF crText = RGB(10, 10, 10);
		if (lplvcd->iSubItem == 1)
		{
			crText = RGB(50, 50, 50);
			lplvcd->clrText = crText;
			*pResult = CDRF_DODEFAULT;
			return;
		}
	}
	*pResult = 0;
}


void CProjectAADlg::OnBnClickedButton5()
{
	if (m_cUserCheckDlg != nullptr)
	{
		delete m_cUserCheckDlg;
		m_cUserCheckDlg = nullptr;
	}
	if (m_cUserCheckDlg == nullptr)
	{
		m_cUserCheckDlg = new CUserCheckDlg;
		m_cUserCheckDlg->Create(IDD_DIALOG_USER, this);
	}
	m_cUserCheckDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnOpenMesDlg()
{
	if (m_cTcpipDlg != nullptr)
	{
		delete m_cTcpipDlg;
		m_cTcpipDlg = nullptr;
	}
	if (m_cTcpipDlg == nullptr)
	{
		m_cTcpipDlg = new CTcpipDlg;
		m_cTcpipDlg->Create(IDD_DIALOG_TCPIP, this);
	}
	m_cTcpipDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::InitUi()
{
	SetDlgItemText(IDC_STATIC_101, gSystemMgr.productName);

	//

	LONG lStyle;
	m_listSave.SetBkColor(RGB(200, 220, 250));
	lStyle = GetWindowLong(m_listSave.m_hWnd, GWL_STYLE);
	lStyle &= ~LVS_TYPEMASK;
	lStyle |= LVS_REPORT;
	SetWindowLong(m_listSave.m_hWnd, GWL_STYLE, lStyle);
	m_listSave.ShowWindow(SW_SHOW);
	DWORD dwStyle = m_listSave.GetExtendedStyle();
	dwStyle |= LVS_EX_FULLROWSELECT;
	dwStyle |= LVS_EX_GRIDLINES;
	dwStyle |= LVS_EX_INFOTIP;

	CFont                      m_logFont;
	double m_fScreenX = 1;
	m_logFont.CreateFont(int(30 * m_fScreenX), 0, 0, 0, FW_NORMAL, FALSE, FALSE, 0,
		ANSI_CHARSET,
		OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY,
		DEFAULT_PITCH | FF_SWISS,
		_T("Arial"));

	m_listSave.SetExtendedStyle(dwStyle);
	m_listSave.SetFont(&m_logFont);
	m_listSave.EnableWindow(TRUE);
	m_listSave.InsertColumn(0, L"SN@成品码", LVCFMT_LEFT, 240);
	m_listSave.InsertColumn(1, L"开始时间", LVCFMT_LEFT, 90);
	m_listSave.InsertColumn(3, L"结果", LVCFMT_LEFT, 90);
	m_listSave.InsertColumn(4, L"操作员", LVCFMT_LEFT, 90);
	m_listSave.InsertColumn(5, L"产品型号", LVCFMT_LEFT, 90);
	m_listSave.InsertColumn(6, L"载具型号", LVCFMT_LEFT, 90);
	m_listSave.InsertColumn(7, L"程序", LVCFMT_LEFT, 90);
	m_listSave.ShowWindow(SW_HIDE);
	m_listSave.EnableWindow(TRUE);
	m_listSave.Invalidate();
	m_listSave.ShowWindow(SW_SHOW);
	m_listSave.BringWindowToTop();

	gSystemMgr.report.fileFlodePath.Format(L"%s\\report\\%s", 
		gSystemMgr.appPath.GetString(), gSystemMgr.productName.GetString());
	gSystemMgr.report.reportType = L"leidiao";
	SetDlgItemTextW(IDC_STATIC_101, gSystemMgr.productName);
	SetWindowText(L"丽景4线 镭雕 - V1.5");

	gSystemMgr.ReadCPM();
}

void CProjectAADlg::OnHandleOpenSetBarcode()
{
	if (m_cSetSnDlg != nullptr)
	{
		delete m_cSetSnDlg;
		m_cSetSnDlg = nullptr;
	}
	if (m_cSetSnDlg == nullptr)
	{
		m_cSetSnDlg = new CSetSnDlg;
		m_cSetSnDlg->Create(IDD_SET_BARCODE, this);
	}
	m_cSetSnDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnBnClickedCheckEndNoScan()
{
	gSystemMgr.endNoScan = !gSystemMgr.endNoScan;
}
